#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<unistd.h>
#include<pthread.h>

using namespace std;
#define NUM 4
int tickets = 1000;//用多线程模拟一轮抢票

class threadData
{
public:
    threadData(int number,pthread_mutex_t* mutex)
    {
        threadname = "thread" + to_string(number);
        lock = mutex;
    }
public:
    string threadname;
    pthread_mutex_t * lock;
};

void* getTicket(void* args)
{
    threadData* td = static_cast<threadData*> (args);
    const char* name = td->threadname.c_str();
    while(true)
    {
        pthread_mutex_lock(td->lock);   //申请锁成功,才能往后执行,不成功,阻塞等待
        if(tickets > 0)
        {
            sleep(1);
            printf("who = %s,get a ticket: %d\n",name,tickets);
            tickets--;
            pthread_mutex_unlock(td->lock);
        }
        else{
            pthread_mutex_unlock(td->lock);
            break;
        }
        usleep(13);
    }

    return 0;
}

int main()
{
    pthread_mutex_t lock;
    pthread_mutex_init(&lock,nullptr);

    vector<pthread_t> tids;
    vector<threadData*> thread_datas;
    for(int i = 1;i < NUM;i++)
    {

        pthread_t tid;
        threadData* td = new threadData(i,&lock);
        thread_datas.push_back(td);
        pthread_create(&tid,nullptr,getTicket,thread_datas[i-1]);
        tids.push_back(tid);
    }

    for(auto thread:tids)
    {
        pthread_join(thread,nullptr);
    }
    for(auto td:thread_datas )
    {
        delete td;
    }


    pthread_mutex_destroy(&lock);

    return 0;
}
